package com.purong.purong.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.adapter.HelpQuestionAdapter;
import com.purong.purong.bean.ConfigBean;
import com.purong.purong.bean.CreditCollectBean;
import com.purong.purong.bean.HelpQuestionBean;
import com.purong.purong.bean.QuestionBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.widget.ConfirmDialog;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrCustomHandler;
import in.srain.cube.views.ptr.PtrDefaultCustomHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrPuRongLayout;

/**
 * Created by zengwenjie on 2017/12/6.
 */
public class HelpActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private PtrPuRongLayout mPtrFrame;
    private LoadingView loadView;
    private HelpQuestionAdapter mAdapter;
    private List<QuestionBean> questionList=new ArrayList<>();
    private int page = 1;
    private View head;
    @Override
    public int getRootViewId() {
        return R.layout.activity_help;
    }

    @Override
    public void initView() {
        head=getLayoutInflater().inflate(R.layout.help_head,null);
        head.findViewById(R.id.top_img).setLayoutParams(new LinearLayout.LayoutParams(CommonUtil.getScreenWidth(this),(int)(CommonUtil.getScreenWidth(this)/3.75)));
        head.findViewById(R.id.small_loan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(HelpActivity.this,HelpCatActivity.class);
                intent.putExtra("TYPE","3");
                startActivity(intent);
            }
        });
        head.findViewById(R.id.bank_loan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(HelpActivity.this,HelpCatActivity.class);
                intent.putExtra("TYPE","2");
                startActivity(intent);
            }
        });
        head.findViewById(R.id.credit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(HelpActivity.this,HelpCatActivity.class);
                intent.putExtra("TYPE","1");
                startActivity(intent);
            }
        });
        head.findViewById(R.id.account).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(HelpActivity.this,HelpCatActivity.class);
                intent.putExtra("TYPE","4");
                startActivity(intent);
            }
        });
        loadView = (LoadingView) head.findViewById(R.id.loading_view);
        loadView.setNoResultToast("暂无问题", "");
        loadView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getQuestion(true);
            }

            @Override
            public void onRefresh() {
                getQuestion(true);
            }
        });
        mRecyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter=new HelpQuestionAdapter();
        mAdapter.setData(questionList);
        mAdapter.addHeaderView(head);
        mAdapter.setOnQuestionListener(new HelpQuestionAdapter.OnQuestionListener() {
            @Override
            public void onClick(int position) {
                Intent intent=new Intent(HelpActivity.this,HelpQuestDetActivity.class);
                intent.putExtra("QUESTION",questionList.get(position));
                startActivity(intent);
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mPtrFrame = (PtrPuRongLayout) findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setPtrHandler(new PtrCustomHandler() {
            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                if (questionList.size() == 0) {
                    return false;
                } else {
                    return PtrDefaultCustomHandler.checkContentCanBePulledUp(frame, content, footer);
                }

            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getQuestion(false);

            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getQuestion(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return false;
//                return (isRequesting||loanList.size()>0)?false:true;
            }
        });

    }
    private void getQuestion(boolean clear){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (clear){
            page=1;
            questionList.clear();
            mAdapter.notifyDataSetChanged();
            loadView.loading();
        }
        params.put("page", page+"");
        if (!request(Contant.QUESTION, params, true)) {
            if (clear)
                loadView.fail();
            if (!hasDestroy())
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (mPtrFrame.isRefreshing()) {
                            mPtrFrame.refreshComplete();
                        }
                    }
                }, 100);
        }
    }
    private void getServicePhone(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("config_code", "ServiceTel");
        request(Contant.CONFIG, params, true,true);
    }
    @Override
    public void initData() {
        getQuestion(true);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.QUESTION)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    HelpQuestionBean bean= JSON.parseObject(responseInfo.content,HelpQuestionBean.class);
                    if (bean != null && bean.getQuestion_list() != null) {
                        if (bean.getQuestion_list().size() > 0)
                            page++;
                        questionList.addAll(bean.getQuestion_list());
                        mAdapter.notifyDataSetChanged();

                    }
                    if (questionList.size() == 0) {
                        loadView.noResultWithRefresh();

                    } else {
                        loadView.finish();
                    }
                }catch (Exception e){
                    if (page == 1) {
                        loadView.fail();
                    }
                }
            }else {
                if (page == 1) {
                    loadView.fail();
                }
            }
        }else if (responseInfo.url.equals(Contant.CONFIG)){
            if (responseInfo.isRequestOk){
                try{
                    final ConfigBean bean= JSON.parseObject(responseInfo.content,ConfigBean.class);
                    if (bean!=null&&bean.getConfig()!=null&&bean.getConfig().getServiceTel()!=null){
                        ConfirmDialog dialog=new ConfirmDialog(this, "确定拨打"+bean.getConfig().getServiceTel()+"?", new ConfirmDialog.onConfirmClickListenr() {
                            @Override
                            public void onConfirm() {
                                Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+bean.getConfig().getServiceTel()));
                                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(callIntent);
                            }

                            @Override
                            public void onCancel() {

                            }
                        });
                        dialog.show();
                    }
                }catch (Exception e){

                }
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.right_tv:
                Intent intent;
                if (PuRongApp.getLoginInfo(this)!=null){
                    intent=new Intent(this, HelpMyQuestActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(this, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.service_tel:
                getServicePhone();
                break;
            case R.id.suggest:
                Intent suggestIntent=new Intent(this,SuggestActivity.class);
                startActivity(suggestIntent);
                break;
            case R.id.left_back:
                finish();
                break;
        }
    }
}
